/*
 * @lc app=leetcode.cn id=482 lang=csharp
 *
 * [482] 密钥格式化
 */

// @lc code=start
public class Solution {
    
    char ToUpper(char c)
    {
        byte n = (byte)c;
        if (n >= 97 && n <= 122)
        {
            n &= 0xDF;
        }
        return (char)n;
    }
    public string LicenseKeyFormatting(string s, int k) 
    {
        var sb = new StringBuilder();
        int counter = k;
        for (int i = s.Length - 1; i >= 0; --i)
        {
            if (s[i] == '-')
            {
                continue;
            }
            if (counter == 0)
            {
                sb.Append('-');
                counter = k;
            }
            sb.Append(ToUpper(s[i]));
            --counter;
        }
        var result = sb.ToString().ToCharArray();
        Array.Reverse(result);
        return new string(result);
    }
}
// @lc code=end

